package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.PowerManager
import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity


class ActivityOnePix : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 放在左上角
        window.setGravity(Gravity.START or Gravity.TOP)
        val params = window.attributes
        // 起始位置
        params.x = 0
        params.y = 0
        // 宽高1px
        params.height = 1
        params.width = 1
        window.attributes = params

        // 在一像素activity里注册广播接受者    接受到广播结束掉一像素
        val intentFilter = IntentFilter()
        intentFilter.addAction("finish")
        registerReceiver(receive, intentFilter)
        checkScreen()
    }


    private val receive = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(receive)
    }

    override fun onResume() {
        super.onResume()
        checkScreen()
    }

    /**
     * 检查屏幕是否点亮
     */
    private fun checkScreen() {
        val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
        val isScreenOn = pm.isInteractive  // SDK < 21 -> pm.isScreeOn
        if (isScreenOn) {
            finish()
        }
    }
}
